package com.ruoyi.web.controller.payment.ceshi;

import com.ruoyi.common.utils.uuid.UUID;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/ceshi")
@CrossOrigin
public class WxFenXiang {


    private final Logger log = LoggerFactory.getLogger(this.getClass());
//    http%3A%2F%2Flocalhost%3A8083%2F
    @RequestMapping(value = "/getAccessTokenAndJsapi",method = RequestMethod.GET)
    public CommonResponse getAccessTokenAndJsapi(@RequestParam(value = "url",defaultValue = "http%3A%2F%2Flocalhost%3A8083%2F",required = false) String url){
        log.info(" url = {} ",url);
        // 1. 获取access_token
        String accessToken = WxTokenAndJsapiUtil.getAccessToken(WxTokenAndJsapiUtil.appId,WxTokenAndJsapiUtil.appSecret);
        log.info(" accessToken = {} ",accessToken);
        // 2. 获取access_ticket
        String accessTicket = WxTokenAndJsapiUtil.getAccessTicket(accessToken);
        log.info(" accessTicket = {} ",accessTicket);
        // 3. 获取时间戳和随机字符串
        String nonceStr = UUID.randomUUID().toString().replace("-", "").substring(0, 16);//随机字符串
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);//时间戳
        log.info("nonceStr = {} ,  timestamp = {}",nonceStr,timestamp);
        // 4. 获取url
        if (!StringUtils.isEmpty(url) && url.length() > 0 ){
        }else {
            url = WxTokenAndJsapiUtil.url;
        }
        // 5. 将参数排序并拼接字符串
        String str = "jsapi_ticket="+accessTicket+"&noncestr="+nonceStr+"&timestamp="+timestamp+"&url="+url;
        // 6. 将字符串进行sha1加密
        String signature = WxTokenAndJsapiUtil.SHA1(str);
        log.info(" signature = {} ",signature);
        Map<String,String> map=new HashMap();
        map.put("appId",WxTokenAndJsapiUtil.appId);
        map.put("timestamp",timestamp);
        map.put("accessToken",accessToken);
        map.put("ticket",accessTicket);
        map.put("nonceStr",nonceStr);
        map.put("signature",signature);
        JSONObject jsonObject = JSONObject.fromObject(map);
        return ResponseVoUtil.success(jsonObject);
    }



}
